define(['jquery', 'bootstrap', 'backend', 'table', 'form','selectpage'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wechat/membercard/index',
                    add_url: 'wechat/membercard/add',
                    edit_url: 'wechat/membercard/edit',
                    del_url: 'wechat/membercard/del',
                    multi_url: 'wechat/membercard/multi',
                    table: 'wechat_member_card',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        //{field: 'mp_id', title: __('Mp_id')},
                        //{field: 'card_id', title: __('Card_id')},
                        //{field: 'code_type', title: __('Code_type')},
                        //{field: 'logo_url', title: __('Logo_url'), formatter: Table.api.formatter.url},
                        //{field: 'background_pic_url', title: __('Background_pic_url'), formatter: Table.api.formatter.url},
                        {field: 'brand_name', title: __('Brand_name')},
                        {field: 'title', title: __('Title')},
                        //{field: 'color', title: __('Color')},
                        //{field: 'notice', title: __('Notice')},
                        //{field: 'service_phone', title: __('Service_phone')},
                        //{field: 'description', title: __('Description')},
                        //{field: 'date_type_text', title: __('Date_type'), operate:false},
                        //{field: 'begin_timestamp', title: __('Begin_timestamp')},
                        //{field: 'end_timestamp', title: __('End_timestamp')},
                        //{field: 'fixed_term', title: __('Fixed_term')},
                        //{field: 'fixed_begin_term', title: __('Fixed_begin_term')},
                        //{field: 'quantity', title: __('Quantity')},
                        //{field: 'location_id_list', title: __('Location_id_list')},
                        //{field: 'get_limit', title: __('Get_limit')},
                        //{field: 'use_custom_code_text', title: __('Use_custom_code'), operate:false},
                        //{field: 'get_custom_code_mode_text', title: __('Get_custom_code_mode'), operate:false},
                        //{field: 'can_give_friend_text', title: __('Can_give_friend'), operate:false},
                        //{field: 'custom_url_name', title: __('Custom_url_name')},
                        //{field: 'custom_url_sub_title', title: __('Custom_url_sub_title')},
                        //{field: 'custom_url', title: __('Custom_url'), formatter: Table.api.formatter.url},
                        //{field: 'discount', title: __('Discount')},
                        //{field: 'promotion_url_name', title: __('Promotion_url_name')},
                        //{field: 'promotion_url', title: __('Promotion_url'), formatter: Table.api.formatter.url},
                        {field: 'create_time', title: __('Create_time'), formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), formatter: function(res){
                            return res==1? '审核通过': '审核未通过';
                        }},
                        {field: 'id', title: __('Operate'), table: table, buttons: [
                            {
                                name: 'detail',
                                text: __('Detail'),
                                title: __('Detail'),
                                icon: 'fa fa-list',
                                classname: 'btn btn-xs btn-success btn-dialog', url: 'wechat/membercard/edit'},
                            {
                                name: 'activate_form',
                                text: __('Activate_form'),
                                title: __('Activate_form'),
                                icon: 'fa fa-list',
                                classname: 'btn btn-xs btn-primary btn-dialog', url: 'wechat/membercard/setForm'},
                            {
                                name: 'detail',
                                text: __('Del'),
                                title: __('Del'),
                                icon: 'fa fa-trash',
                                classname: 'btn btn-xs btn-danger btn-delone', url: 'wechat/membercard/del'}
                        ], events: Table.api.events.operate, formatter: Table.api.formatter.buttons,operate:false}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            addOrEditEvent();
        },
        edit: function () {
            Controller.api.bindevent();
            addOrEditEvent();
        },
        setform: function () {
            Controller.api.bindevent();
            initSelectPage();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});

var initSelectPage = function(){
    //1.模拟数据，当然你也可以在selectPage(初始化方法)的参数(option)里增加source选项，利用ajax请求数据
    var tag_data = [
        {index: 0 ,id:'USER_FORM_INFO_FLAG_MOBILE' ,desc:'手机号'},
        {index: 1 ,id:'USER_FORM_INFO_FLAG_SEX' ,desc:'性别'},
        {index: 2 ,id:'USER_FORM_INFO_FLAG_NAME' ,desc:'姓名'},
        {index: 3 ,id:'USER_FORM_INFO_FLAG_BIRTHDAY' ,desc:'生日'},
        {index: 4 ,id:'USER_FORM_INFO_FLAG_IDCARD' ,desc:'身份证'},
        {index: 5 ,id:'USER_FORM_INFO_FLAG_EMAIL' ,desc:'邮箱'},
        {index: 6 ,id:'USER_FORM_INFO_FLAG_LOCATION' ,desc:'详细地址'},
        {index: 7 ,id:'USER_FORM_INFO_FLAG_EDUCATION_BACKGROUND' ,desc:'教育背景'},
        {index: 8 ,id:'USER_FORM_INFO_FLAG_INDUSTRY' ,desc:'行业'},
        {index: 9 ,id:'USER_FORM_INFO_FLAG_INCOME' ,desc:'收入'},
        {index: 10 ,id:'USER_FORM_INFO_FLAG_HABIT' ,desc:'兴趣爱好'}
    ];


    $('#req-common_field_id_list,#opt-common_field_id_list').selectPage({
        showField : 'desc',//showField：设置下拉列表中显示文本的列
        keyField : 'id',//keyField：设置下拉列表项目中项目的KEY值，用于提交表单,会保存在被初始化的input标签的value属性中
        orderBy : 'index',//这里就是按照tag_data的id进行倒序排列
        data : tag_data
    });
};

var addOrEditEvent = function(){
    //时间类型
    $("#c-date_type").on('change',function(){
        $("#date-block1").toggleClass("hidden");
        $("#date-block2").toggleClass("hidden");
    });

    //支持积分
    $("#supply_bonus").on('change',function(){
        $("#bonus-rule-block").toggleClass("hidden");
    });

    //激活方式
    $("input[id^='row[activate_type]-']").on('click',function(){
        var type = $(this).val();
        $("#auto_activate").val(type == 'auto');
        switch(type){
            case 'app':
                $("#activate-block-url").removeClass("hidden");
                $("#activate-block-app").removeClass("hidden");
                $("#activate-block-component").addClass("hidden");
                break;
            case 'url':
                $("#activate-block-url").removeClass("hidden");
                $("#activate-block-app").addClass("hidden");
                $("#activate-block-component").addClass("hidden");
                break;
            case 'component':
                $("#activate-block-url").addClass("hidden");
                $("#activate-block-app").addClass("hidden");
                $("#activate-block-component").removeClass("hidden");
                break;
            default:
                $("[id^='activate-block-']").addClass("hidden");
        }
    });

    //激活后跳转
    $("input[id^='row[wx_activate_after_submit]-']").on('click',function(){
        $("#activate-blo-jump").toggleClass("hidden");
    });
};